Implement Checksum type for binary checksums
authorFelix Krull <f_krull@gmx.de>
Sat, 31 Aug 2019 20:40:05 +0000 (22:40 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:54 +0000 (12:53 -0400)
rust-bindings/rust/src/checksum.rs [new file with mode: 0644]
rust-bindings/rust/src/lib.rs

diff --git a/rust-bindings/rust/src/checksum.rs b/rust-bindings/rust/src/checksum.rs
new file mode 100644 (file)
index 0000000..9f07c7d
--- /dev/null
@@ -0,0 +1,39 @@
+use glib::translate::{from_glib_full, FromGlibPtrFull};
+use glib::GString;
+use glib_sys::{g_free, gpointer};
+use std::fmt;
+
+pub struct Checksum {
+    bytes: *mut [u8; 32],
+}
+
+impl Checksum {
+    pub(crate) unsafe fn new(bytes: *mut [u8; 32]) -> Checksum {
+        assert!(!bytes.is_null());
+        Checksum { bytes }
+    }
+
+    fn to_gstring(&self) -> GString {
+        unsafe { from_glib_full(ostree_sys::ostree_checksum_from_bytes(self.bytes)) }
+    }
+}
+
+impl Drop for Checksum {
+    fn drop(&mut self) {
+        unsafe {
+            g_free(self.bytes as gpointer);
+        }
+    }
+}
+
+impl FromGlibPtrFull<*mut [u8; 32]> for Checksum {
+    unsafe fn from_glib_full(ptr: *mut [u8; 32]) -> Self {
+        Checksum::new(ptr)
+    }
+}
+
+impl fmt::Display for Checksum {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.to_gstring())
+    }
+}
index 50bf269f895168d069e8402a44aa01552246a432..9bc4a8e836766a2fd138fd0adef047e139087845 100644 (file)
@@ -34,6 +34,7 @@ pub use crate::auto::functions::*;
 pub use crate::auto::*;
 
 // handwritten code
+mod checksum;
 #[cfg(any(feature = "v2018_6", feature = "dox"))]
 mod collection_ref;
 #[cfg(any(feature = "v2019_3", feature = "dox"))]
@@ -42,6 +43,7 @@ mod object_name;
 mod repo;
 #[cfg(any(feature = "v2018_2", feature = "dox"))]
 mod repo_checkout_at_options;
+pub use crate::checksum::*;
 #[cfg(any(feature = "v2018_6", feature = "dox"))]
 pub use crate::collection_ref::*;
 #[cfg(any(feature = "v2019_3", feature = "dox"))]